package com.neusoft.crm.service.impl;

import com.neusoft.crm.mapper.SalePlanMapper;
import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.po.SalePlan;
import com.neusoft.crm.service.ISalePlanService;
import com.neusoft.crm.utils.Slf4j2Logger;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户开发计划接口实现类
 *
 * @author msprc
 */
@Service
public class SalePlanServiceImpl implements ISalePlanService {
    @Autowired
    private SalePlanMapper salePlanMapper;

    @Override
    @Cacheable(value = "locateSalePlanList", key = "'planList:'+#salePlan.saleChance.chanceCustName +'planTodo'+ #salePlan.planTodo", unless = "#result==null")
    public List<SalePlan> selectSalePlanList(SalePlan salePlan) {
        return salePlanMapper.selectSalePlanList(salePlan);
    }

    @Override
    @CacheEvict(value = "locateSalePlanList", allEntries = true)
    public int createSalePlan(SalePlan salePlan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = salePlanMapper.createSalePlan(salePlan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("开发计划创建失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "locateSalePlanList", allEntries = true)
    public int updateSalePlanList(List<SalePlan> salePlan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = salePlanMapper.updateSalePlanList(salePlan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("开发计划更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "locateSalePlanList", allEntries = true)
    public int deleteSalePlan(SalePlan salePlan) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = salePlanMapper.deleteSalePlan(salePlan);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("开发计划删除失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "locateSalePlanList", key = "'saleChanceId:'+#saleChance.chanceId")
    public List<SalePlan> getSalePlanListBySaleChanceId(SaleChance saleChance) {
        return salePlanMapper.getSalePlanListBySaleChanceId(saleChance);
    }


}
